# -*- coding: utf-8 -*-
from flask import current_app
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from info import create_app, db
from info import food_models
from info.food_models import Admin

app = create_app('development')



manager = Manager(app)
# 将app与db关联
Migrate(app, db)
# 将命令迁移到manager上
manager.add_command('db', MigrateCommand)


@manager.option("-n", "-name", dest="name")
@manager.option("-p", "-password", dest="password")
def createadmin(name, password):

    if not all([name, password]):
        print("name or password is null")
    admin = Admin()
    admin.username = name
    admin.password = password
    try:
        db.session.add(admin)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        print(e)
    print("super user add success")


if __name__ == '__main__':
    print(app.url_map)
    manager.run()
